export default {
	data() {
		return {

		}
	},
	methods: {
		commonGetwxCode() {
			return new Promise((resolve, reject) => {
				let _this = this
				// #ifdef MP-WEIXIN
				uni.login({
					success(res) {
						let code = res.code
						console.log(code);
						uni.showModal({
							title: '温馨提示',
							content: '亲，授权微信登录后才能正常使用小程序功能',
							success(res) {
								//如果用户点击了确定按钮
								if (res.confirm) {
									uni.getUserProfile({
										desc: '请允许登录以获取你的昵称、头像',
										success: res => {
											console.log(res);
											 let info = _this.commonMpWxLogin(code, res.userInfo)
											 resolve(info)
										},
										fail: res => {
											//拒绝授权
											uni.showToast({
												title: '您拒绝了请求,不能正常使用小程序!',
												icon: 'none',
												duration: 3000
											});
											return;
										}
									});
								} else if (res.cancel) {
									//如果用户点击了取消按钮
									uni.showToast({
										title: '您拒绝了请求,不能正常使用小程序!',
										icon: 'none',
										duration: 3000
									});
									return;
								}
							}
						});
					}
				})
				// #endif
			})

		},
		async commonMpWxLogin(_code, _userInfo) {
			let form = {}
			form.code = _code
			form.userInfo = _userInfo
			let tjr_id = uni.getStorageSync('tjr_id')
			if (tjr_id > 0) {
				form.tjr_id = tjr_id
			}
			await this.myhttp({
				url: '?s=/api/Passport/mpWxLogin',
				data: {
					form
				},
			}).then(res => {
				uni.setStorageSync('Access-Token', res.data.data.token);
				this.$store.state.user.user_id = res.data.data.userId
				this.$store.state.user.token = res.data.data.token
				return this.commonGetUserInfo()
			})
		},
		async commonGetUserInfo() {
			await this.myhttp({
				url: '?s=/api/User/info',
			}).then(res => {
				if (res.data.message == 'success') {
					uni.setStorageSync('userInfo', res.data.data.userInfo);
					uni.showToast({
						title: '登录成功，请继续操作',
						icon: 'none',
						duration: 3000,
					})
					return res.data
				}
			})
		},
	}
}
